package com.taojiazheng.controller.Order;

import com.taojiazheng.common.ResultResponse;
import com.taojiazheng.entity.Order;
import com.taojiazheng.service.order.OrderService;
import com.taojiazheng.so.order.OrderSo;
import com.taojiazheng.vo.Order.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 赵成林
 * @date 2017/11/25 17:33
 * 用户订单的操作
 * 包括创建订单
 * 支付等
 */
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    /**
     * 用户创建订单
     * 创建过订单，但没有支付的，不能继续创建
     * 初始创建订单状态 0
     * 订单失效（商品已经卖光）状态   -1
     * 完成支付 状态 1
     * 确认工作完成 状态 2
     */
    @ResponseBody
    @RequestMapping(value = "createorder",method = RequestMethod.POST)
    public ResultResponse createOrder(@RequestBody OrderSo orderSo, HttpServletRequest request) {
        int orderId = orderService.createOrder(orderSo,request.getHeader("token"));
        Order order = new Order();
        order.setId(orderId);
        return new ResultResponse(order);
    }

    /**
     * 获取订单
     * 前端如果传递一个空对象
     * 就获得一个用户的订单列表
     * @param orderSo
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getOrder",method = RequestMethod.POST)
    public ResultResponse<OrderVo> getOrder(@RequestBody OrderSo orderSo,HttpServletRequest request){
        //return orderService.getOrder(orderSo,request.getHeader("tok en"));
        List<OrderVo> voList = orderService.getOrder(orderSo,request.getHeader("token"));
        return new ResultResponse<OrderVo>(voList);
    }
    /**
     * 获取订单详情
     * 传入订单id
     */
    @ResponseBody
    @RequestMapping(value = "getOrderInfo",method = RequestMethod.POST)
    public ResultResponse getOrderInfo(@RequestBody OrderSo orderSo,HttpServletRequest request){
        List<OrderVo> voList = orderService.getOrder(orderSo,request.getHeader("token"));
        return new ResultResponse(voList);
    }
    /**
     * 对一个订单进行支付
     * 改变订单的状态
     * 添加用户的消费金额
     * 根据消费金额判断用户是不是可以获得优惠卷（消费大于30元）
     * 工作的需求人数-1
     */
    @ResponseBody
    @RequestMapping(value = "pay",method = RequestMethod.POST)
    public ResultResponse pay(@RequestBody OrderSo orderSo,HttpServletRequest request){
        String token = request.getHeader("token");
        orderService.updateOrder(orderSo,token);
        return new ResultResponse();
    }
    /**
     * 确认工作的完成
     */
    @ResponseBody
    @RequestMapping(value = "finished",method = RequestMethod.POST)
    public  ResultResponse finish(@RequestBody OrderSo orderSo){
        orderService.updateOrderFinished(orderSo);
        return new ResultResponse();
    }
    @ResponseBody
    @RequestMapping(value = "delete/order",method = RequestMethod.POST)
    public ResultResponse deleteOrder(@RequestBody OrderSo so){
        orderService.deleteOrder(so.getId());
        return new ResultResponse();
    }
}
